package org.balthie.cal.impl.cmd.spec;

import org.balthie.cal.common.ex.InputException;
import org.balthie.cal.core.ICalKind.CalKind;
import org.balthie.cal.core.ICommand;
import org.balthie.cal.core.IMathOperator.MathOperatorType;
import org.balthie.cal.impl.cmd.GenericMathOptCmd;

public class CurrentCmdSpecs {

	public static final CurrentCmdSpec IS_MATHOPT = new CurrentCmdSpec(CalKind.MathOperator);

	public static final CurrentCmdSpec IS_MATHOPT_EQUALS = new CurrentCmdSpec(MathOperatorType.Equals);

	public static class CurrentCmdSpec extends AbsAtomicCmdSpec {

		private CalKind expectKind;
		private MathOperatorType expectMathOptType;

		private CurrentCmdSpec(CalKind expectKind) {
			this.expectKind = expectKind;
		}

		public CurrentCmdSpec(MathOperatorType type) {
			this.expectMathOptType = type;
		}

		@Override
		protected boolean doCheck(ICommand candidate, boolean isThrow) {
			boolean isSatisfied = (this.expectKind == null || candidate.getCalKind() == this.expectKind)
					&&  (this.expectMathOptType == null || ((GenericMathOptCmd) candidate).getOperator() == this.expectMathOptType);

			if (!isSatisfied && isThrow) {
				throw new InputException(String.format("ERROR: %s current command [expect %s] [actually %s] ", this.getClass().getSimpleName(),
						this.expectKind !=null?this.expectKind: this.expectMathOptType, candidate.toString() ));
			} else {
				return isSatisfied;
			}
		}
	}
}
